//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    movies: [
    ] ,
    display: "none",
    address: '智慧社区',
    cate: '',
  },
  onShow:function(){
    let that=this;
    if (app.data.address != '' && app.data.address != undefined) {
      that.setData({
        address: app.data.address
      })
    }
    wx.setStorageSync('classlist', '')
    wx.request({
      url: app.data.Url + 'menus/menus',
      data:{
        bid: wx.getStorageSync('bid')
      },
      method: 'get',
      success: function (res) {
        console.log(res)
        that.setData({
          cate:res.data.data
        })
      }
    })
    wx.request({
      url: app.data.Url + 'goods/ifyAndgoods',
      data: {
        bid: wx.getStorageSync('bid'),
        limit: 4
      },
      method: 'get',
      success: function (res) {
        that.setData({
          goods: res.data.data
        })
        console.log(res.data.data)
      }
    })
    wx.request({
      url: app.data.Url + 'banner/banlist',
      data: {
        bid: wx.getStorageSync('bid'),
        type: 0
      },
      success: function (res) {
        that.setData({
          movies: res.data.data
        })
      }
    })
  },
  onLoad: function () {
    var that = this;
    if (!wx.getStorageSync('userInfo')) wx.hideTabBar({

    });
    wx.setStorageSync('classlist','')
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        var latitude = res.latitude
        var longitude = res.longitude
        wx.request({
          url: app.data.Url + 'wisdom/get_add',
          data: {
            latitude: latitude,
            longitude: longitude
          },
          method: 'post',
          success: function (res) {

            app.data.address = res.data.data.name
            wx.setStorageSync('bid', res.data.data.sqid)
            that.setData({
              address: app.data.address
            })
          }
        })
      }
    })


    if (!wx.getStorageSync('userInfo')) {
      that.setData({
        logo: true
      })

    }

  },
  sendRequest: function (res) {
    var that = this;
    if (!res.detail.rawData) {
      wx.showModal({
        title: '提示',
        content: '请重新点击授权',
      })
      wx.hideLoading()
      return false;
    }
    wx.showLoading({
      title: '请稍后',
    })
    wx.login({

      success: function (code) {

        var code = code.code;
        wx.request({
          url: app.data.Url + 'User/logins',
          data: {
            utoken: '',
            code: code,
            encryptedData: res.detail.encryptedData,
            iv: res.detail.iv,
            bid: 123
          },
          method: 'post',
          success: function (rs) {
            if (rs.data.success == 10) {
              wx.request({
                url: app.data.Url + 'User/getUid',
                data: {
                  openid: rs.data.openid
                },
                method: 'post',
                success: function (e) {
                  if (!e.data.data) wx.showModal({
                    title: '提示',
                    content: '请重新点击授权',
                  })
                  wx.hideLoading()
                  wx.setStorageSync('userInfo', {
                    head: res.detail.userInfo.avatarUrl,
                    name: res.detail.userInfo.nickName,
                    sex: res.detail.userInfo.gender,
                    uid: e.data.data,
                    openid: rs.data.openid
                  })
                
                  wx.setStorageSync('phone', e.data.phone)
           
                  wx.showTabBar({
                    success: function () {
                      that.setData({
                        logo: false
                      })
                      console.log(1111111111111111111111111111)
                      that.onShow()
                      wx.showTabBar({
                        
                      })
                      wx.hideLoading()
                    }
                  })

                }
              })
            } else {
              wx.showModal({
                title: '提示',
                content: '请重新点击授权',
              })
              wx.hideLoading()
              return false;
            }
          }
        })
      }
    })
  },
  clickurl: function (e) {
    var id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    wx.switchTab({
      url: '../classify/classify',
    })
    wx.setStorage({
      key: 'tid',
      data: id,
    })
    wx.navigateTo({
      url: '../assortment/assortment?id=' + id,
    })
  },
  search:function(){
    wx.navigateTo({
      url: '../search/seller',
    })
  },
})
